package com.oto.web.resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author xueqiang.mi
 * @since 2011-1-17
 */
public class GetDataSupport {

	public static final String SEARCH_URL = "http://www.otophone.com/search?key=";
	public static final String SYNC_URL = "http://www.otophone.com/mobile/sync?user=";

	public static String get(String url, String key) throws IOException {
		HttpURLConnection conn = null;
		BufferedReader br = null;
		try {
			URL serviceUrl = new URL(url + key);
			conn = (HttpURLConnection) serviceUrl.openConnection();
			conn.setDoOutput(false);
			conn.setDoInput(true);
			conn.setRequestMethod("GET");
			conn.setUseCaches(false);

			conn.connect();

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				System.out.println("Error occurs. ResponseCode: " + conn.getResponseCode());
			}
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line.trim());
			}
			return sb.toString();
		} finally {
			if (br != null) {
				br.close();
			}
			conn.disconnect();
		}
	}
}
